package com.zr.controller.car;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zr.exception.BusinessException;
import com.zr.service.car.CarService;
import com.zr.utils.AjaxResult;
import com.zr.vo.car.CarInfo;
import com.zr.vo.dispatch.DispatchTicket;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import java.util.Map;

/**
 * 车辆管理模块的控制器
 *
 * @author 王鑫
 * @date 2022/10/30
 */
@RestController
@RequestMapping("/car")
public class CarController {

    @Resource
    private CarService carService;

    /**
     * 条件查询车辆信息列表，带分页
     *
     * @param carInfo
     * @return AjaxResult
     */
    @RequestMapping("/list")
    public AjaxResult list(@RequestBody CarInfo carInfo) {
        PageHelper.startPage(carInfo.getPageNum(), carInfo.getPageSize());
        List<CarInfo> carInfoList = carService.list(carInfo);
        return AjaxResult.success(200, "查询成功", new PageInfo<>(carInfoList));
    }

    /**
     * 查询所有车辆
     *
     * @return AjaxResult
     */
    @RequestMapping("/listAll")
    public AjaxResult listAll() {
        List<CarInfo> carInfoList = carService.listAll();
        return AjaxResult.success(200, "查询成功", carInfoList);
    }

    /**
     * 新增车辆，增加参数校验功能
     *
     * @param carInfo
     * @return AjaxResult
     */
    @RequestMapping("/add")
    public AjaxResult add(@RequestBody @Validated CarInfo carInfo, BindingResult bindingResult) {
        //参数校验
        if (bindingResult.hasErrors()) {
            Map<String, String> map = new HashMap<>();
            bindingResult.getFieldErrors().forEach((item) -> {
                String msg = item.getDefaultMessage();
                String fie = item.getField();
                map.put(fie, msg);
            });
            return AjaxResult.fail(200, "数据不合法", map);
        }
        carService.add(carInfo);
        return AjaxResult.success(200, "新增成功", null);
    }

    /**
     * 编辑车辆基础信息
     *
     * @param carInfo
     * @return AjaxResult
     */
    @RequestMapping("/edit")
    public AjaxResult edit(@RequestBody CarInfo carInfo) throws BusinessException {
        carService.edit(carInfo);
        return AjaxResult.success(200, "修改成功", null);
    }

    /**
     * 删除车辆
     *
     * @param id
     * @return AjaxResult
     */
    @RequestMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Integer id) throws BusinessException {
        carService.remove(id);
        return AjaxResult.success(200, "删除成功", null);
    }

    /**
     * 查询车辆的详情信息，包含车辆信息，司机信息，调度单信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getCarAndDriver/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        HashMap<String, Object> map = carService.getCarAndDriver(id);
        return AjaxResult.success(200, "查询车辆详情成功", map);
    }

    /**
     * 设置车辆头像
     *
     * @param file
     * @return AjaxResult
     * @throws IOException
     */
    @PostMapping("/setHeadImg")
    public AjaxResult setHeadImg(@RequestParam("file") MultipartFile[] file, @RequestParam("car") String number) throws IOException {
        return AjaxResult.success(200, "上传成功", carService.setHeadImg(file, number));
    }

    /**
     * 根据车牌号，查询车辆的调度单信息(含对应订单信息)
     *
     * @param dispatchTicket
     * @return AjaxResult
     * @throws IOException
     */
    @RequestMapping("/getDispatch")
    public AjaxResult getDispatch(@RequestBody DispatchTicket dispatchTicket) {
        PageHelper.startPage(dispatchTicket.getPageNum(), dispatchTicket.getPageSize());
        List<DispatchTicket> dispatchTicketList = carService.getDispatch(dispatchTicket);
        return AjaxResult.success(200, "查询成功", new PageInfo<>(dispatchTicketList));
    }

    /***
     * 查询在途车辆与空闲车辆数
     * @return AjaxResult
     */
    @RequestMapping("/getCarUsage")
    public AjaxResult getCarUsage() {
        List<Map> list = carService.getCarUsage();
        return AjaxResult.success(200, "获取data成功", list);
    }

    /**
     * 根绝车辆信息获取其异常记录
     *
     * @param carInfo
     * @return
     */
    @RequestMapping("/getExceptions")
    public AjaxResult getExceptions(@RequestBody CarInfo carInfo) {
        List<Map> list = carService.getCarUsage();
        return AjaxResult.success(200, "获取data成功", list);
    }

    /**
     * 根据调度单id获取车辆信息
     *
     * @param dispatchTicket
     * @return AjaxResult
     */
    @RequestMapping("/getCarByNumber")
    public AjaxResult getCarByNumber(@RequestBody DispatchTicket dispatchTicket) {
        CarInfo carInfo = carService.getCarByNumber(dispatchTicket.getCarNumber());
        return AjaxResult.success(200, "获取data成功", carInfo);
    }

    /**
     * 获得车辆与司机的详细信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getDriverAndCar/{id}")
    public AjaxResult getDriverAndCar(@PathVariable("id") Integer id) {
        HashMap<String, Object> map = carService.getDriverAndCar(id);
        return AjaxResult.success(200, "查询车辆详情成功", map);
    }
}
